package com.wen.wenda.controller;
import java.util.Date;

import com.wen.wenda.common.BaseCode;
import com.wen.wenda.common.BaseResponse;
import com.wen.wenda.common.ReviewRequest;
import com.wen.wenda.common.enums.ReviewStatusEnum;
import com.wen.wenda.common.utils.ReturnUtil;
import com.wen.wenda.exception.BusinessException;
import com.wen.wenda.model.domain.App;
import com.wen.wenda.model.request.app.AppCreateRequest;
import com.wen.wenda.model.request.app.AppSearchRequest;
import com.wen.wenda.model.request.app.AppUpdateRequest;
import com.wen.wenda.model.vo.AppVO;
import com.wen.wenda.model.vo.UserVO;
import com.wen.wenda.service.AppService;
import com.wen.wenda.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用接口
 *
 * @author wen
 */
@RestController
@RequestMapping("/app")
public class AppController {

    @Resource
    private AppService appService;


    @Resource
    private UserService userService;

    @PostMapping("/create")
    public BaseResponse<Boolean> createApp(@RequestBody AppCreateRequest appCreateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (appCreateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的添加逻辑
        App app = new App();
        BeanUtils.copyProperties(appCreateRequest, app);
        boolean save = appService.save(app);
        return ReturnUtil.success(save);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteApp(@RequestBody long id, HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        boolean removedById = appService.removeById(id);
        if (!removedById) {
            throw new BusinessException(BaseCode.RESOURCE_NOT_FOUND, "删除失败");
        }
        return ReturnUtil.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateApp(@RequestBody AppUpdateRequest appUpdateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (appUpdateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的修改逻辑
        App app = appService.getById(appUpdateRequest.getId());
        BeanUtils.copyProperties(appUpdateRequest, app);
        boolean res = appService.updateById(app);
        return ReturnUtil.success(res);
    }

    @GetMapping("/list")
    public BaseResponse<List<AppVO>> getAppList(AppSearchRequest appSearchRequest, HttpServletRequest request) {
        if (appSearchRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的查询校验逻辑
        List<App> list = appService.list();
        List<AppVO> appVOList = list.stream().map(item -> {
            AppVO appVO = new AppVO();
            BeanUtils.copyProperties(item, appVO);
            return appVO;
        }).toList();
        return ReturnUtil.success(appVOList);
    }

    /**
     * 审核应用
     *
     * @param reviewRequest 审核请求
     * @param request 前端请求
     * @return 审核成功
     */
    @PostMapping("/review")
    public BaseResponse<Boolean> reviewApp(ReviewRequest reviewRequest, HttpServletRequest request) {
        if (reviewRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        Long id = reviewRequest.getId();
        Integer reviewStatus = reviewRequest.getReviewStatus();
        // 参数校验
        ReviewStatusEnum reviewStatusEnum = ReviewStatusEnum.getEnumByValue(reviewStatus);
        if (id == null || reviewStatusEnum == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR, "请求参数错误");
        }
        // 1. 判断应用是否存在
        App oldApp = appService.getById(id);
        if (oldApp == null) {
            throw new BusinessException(BaseCode.PARAMS_ERROR, "应用不存在");
        }
        // 2. 判断审核状态是否要改变
        if (oldApp.getReviewStatus().equals(reviewStatus)) {
            throw new BusinessException(BaseCode.PARAMS_ERROR, "审核状态未发生改变");
        }
        // 3. 设置审核信息
        App newApp = new App();
        newApp.setId(id);
        newApp.setReviewStatus(reviewStatus);
        String reviewMessage = reviewRequest.getReviewMessage();
        if (StringUtils.isNotBlank(reviewMessage)) {
            newApp.setReviewMessage(reviewMessage);
        }
        UserVO currentUser = userService.getCurrentUser(request);
        newApp.setReviewerId(currentUser.getId());
        newApp.setReviewTime(new Date());
        boolean res = appService.updateById(newApp);
        if (!res) {
            throw new BusinessException(BaseCode.INTERNAL_ERROR, "审核状态更新失败");
        }
        return ReturnUtil.success(true);
    }


}
